<template>
  <div>
    <FormPanel ref="formPanel" :list="formList" :formType="type" @formChange="changeHandle" :isSubmitBtn="false" :collapse="false" labelWidth="110" />
    <div
      :style="{
        position: 'absolute',
        left: 0,
        bottom: 0,
        right: 0,
        zIndex: 9,
        borderTop: '1px solid #e9e9e9',
        padding: '10px 20px',
        background: '#fff',
        textAlign: 'center'
      }"
    >
      <multiuse-button size="mini" type="primary" v-show="type != 'show'" @click="saveHandle">保 存</multiuse-button>
      <el-button @click="closeDrawer">关 闭</el-button>
    </div>
  </div>
</template>
<script>
import moment from 'moment';
import { dictionary } from '@/utils/dictMixin';
import { getDetail, listOrderType, addOrUpdate } from '@/api/omFinance/PriceDiscountManage/ProfitAnalysisCodeMaintain';

export default {
  mixins: [dictionary],
  props: ['type', 'rowData'],

  data() {
    this.saveData = null;
    return {
      formList: this.createdFrom(),
      formData: []
    };
  },
  mounted() {
    this.init();
  },
  methods: {
    init() {
      if (this.type != 'add') {
        this.getInfoById();
      }
      if (this.type == 'show') {
        this.formList.map(x => {
          if (x.rules) {
            x.rules[0].required = false;
          }
        });
      }
    },
    // 详细
    async getInfoById() {
      const res = await getDetail(this.rowData.id);
      if (res.resultCode == 200) {
        this.formList.forEach(x => {
          if (x.fieldName == 'orderTypeSubId') {
            this.getOrderType(res.data['orderType']);
            x.initialValue = res.data[x.fieldName];
          } else {
            x.initialValue = res.data[x.fieldName];
          }
        });
      }
    },
    // 订单细分
    async getOrderType(orderType) {
      const res = await listOrderType({ orderType: orderType });
      if (res.resultCode == 200) {
        this.formList.find(item => item.fieldName == 'orderTypeSubId').itemList = res.data.map(x => ({ text: x.subName, value: x.id }));
      }
    },
    changeHandle(val) {
      this.saveData = val;
    },
    async saveHandle() {
      this.$refs.formPanel.SUBMIT_FORM();
      if (this.saveData !== null) {
        this.saveData.id = this.rowData.id ? this.rowData.id : '';
        const res = await addOrUpdate(this.saveData);
        if (res.resultCode == 200) {
          this.closeDrawer('refresh');
        }
      }
    },
    closeDrawer(type) {
      this.$emit('close', false, type);
    },

    createdFrom() {
      return [
        {
          type: 'SELECT',
          label: '订单类型',
          fieldName: 'orderType',
          placeholder: '请选择',
          selfCols: 1.5,
          itemList: this.createDictList('6006'),
          rules: [{ required: true, message: '请选择', trigger: 'change' }],
          change: val => {
            if (val) {
              this.formList.find(x => x.fieldName == 'orderTypeSubId').itemList = [];
              this.formList.find(x => x.fieldName == 'orderTypeSubId').initialValue = '';
              this.getOrderType(val);
              return;
            }
            this.formList.find(x => x.fieldName == 'orderTypeSubId').itemList = [];
            this.formList.find(x => x.fieldName == 'orderTypeSubId').initialValue = '';
          }
        },
        {
          type: 'SELECT',
          label: '订单细分',
          fieldName: 'orderTypeSubId',
          placeholder: '请选择',
          selfCols: 1.5,
          itemList: [],
          rules: [{ required: true, message: '请选择', trigger: 'change' }]
        },
        {
          type: 'SELECT',
          label: '销售类型',
          fieldName: 'salesType',
          placeholder: '请选择',
          selfCols: 1.5,
          initialValue: '',
          itemList: this.createDictList('6053'),
          rules: [{ required: true, message: '请选择', trigger: 'change' }]
        },
        {
          type: 'SELECT',
          label: '订单原因',
          fieldName: 'orderReason',
          placeholder: '请选择',
          selfCols: 1.5,
          itemList: this.createDictList('6054'),
          rules: [{ required: true, message: '请选择', trigger: 'change' }]
        }
      ];
    }
  }
};
</script>
